package com.lonty.intelligence.service;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.mapper.CertificateMapper;
import com.lonty.intelligence.mapper.HouseholdMapper;
import com.lonty.intelligence.mapper.OwnerMapper;
import com.lonty.intelligence.mapper.UserInfoMapper;
import com.lonty.intelligence.model.Certificate;
import com.lonty.intelligence.model.Household;
import com.lonty.intelligence.model.Owner;
import com.lonty.intelligence.model.UserInfo;
import com.lonty.intelligence.request.RoomInfoList;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.OwnerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/4/17 14:20
 * @Version 1.0
 */
@Service
public class OwnerService {

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private CertificateMapper certificateMapper;

    @Resource
    private HouseholdMapper householdMapper;

    /**
     * 保存业主
     * @param ownerVo
     * @return
     */
    @Transactional
    public Result saveOwner(OwnerVo ownerVo){
        //判断手机号是否存在
//        Owner getOwner = ownerMapper.getByTelephone(ownerVo.getTelephone());
//        if(getOwner != null){
//            return Result.builder(ResultCode.PHONE_IS_EXISTENCE).build();
//        }
        //保存业主
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerVo,owner);
        owner.setId(UUIDUtil.randomID());
        owner.setIsDel(0);
        ownerMapper.saveOwner(owner);
        return Result.builder().data(owner).build();
    }

    /**
     * 根据楼栋ID获取业主详细信息
     * @param buildingId
     * @return
     */
    public Household getOwnerInfo(String buildingId){
        //根据楼栋ID获取房产信息
        Certificate certificate = certificateMapper.getByBuildingId(buildingId);
        //根据ID获取住户信息
        Household household = new Household();
        if(certificate != null){
            household = householdMapper.getById(certificate.getHouseholdId());
        }
        return household;
    }

}
